package com.charger.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.utils.DateUtils;
import com.charger.domain.QChargerStation;
import com.charger.domain.QFailure;
import com.charger.service.QChargerStationService;
import com.charger.service.QFailureService;
import com.charger.utils.QDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName QFailureController
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年11月26日 17:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/gz")
public class QFailureController {

    @Autowired
    private QFailureService failureService;

    @Autowired
    private QChargerStationService stationService;


    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('statistic:gz:list')")
    public Object getList(@RequestParam(defaultValue = "1") int pageNum,
                          @RequestParam(defaultValue = "5") int pageSize,
                          QFailure qFailure){
        Page<QFailure> page = new Page<>(pageNum, pageSize);
        QueryWrapper<QFailure> qw = new QueryWrapper<>();
        qw.between(StringUtils.isNotBlank(qFailure.getStartTime()),
                "failureTime", qFailure.getStartTime(), qFailure.getEndTime())
                .eq(qFailure.getStationId() != null, "charger_station_id", qFailure.getStationId())
                .eq(StringUtils.isNotBlank(qFailure.getStatus()), "state", qFailure.getStatus());
        Page<QFailure> pageInfo = failureService.page(page, qw);
        List<QFailure> records = pageInfo.getRecords();
        records.forEach(fail -> {
            Long stationId = fail.getStationId();
            if (stationId != null) {
                QChargerStation station = stationService.getById(stationId);
                if (station != null) {
                    fail.setQChargerStation(station);
                }
            }
        });
        pageInfo.setRecords(records);
        return AjaxResult.success(pageInfo);
    }

    @GetMapping("/count")
    public Object getCount() throws ParseException {
        Map<String, Object> resMap = new HashMap<>();
        QueryWrapper<QFailure> qw = new QueryWrapper<>();
        String currentDate = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date());
        qw.ge("failureTime", QDateUtils.getFirstTimeOfDay()).le("failureTime", currentDate);
        long dayCount = failureService.count(qw);
        resMap.put("dayCount", dayCount);
        Long weekCount = failureService.query().ge("failureTime", QDateUtils.getCurrentWeekDay())
                .le("failureTime", currentDate).count();
        resMap.put("weekCount", weekCount);
        Long monthCount = failureService.query().ge("failureTime", QDateUtils.getFirstDayOfMonth())
                .le("failureTime", currentDate).count();
        resMap.put("monthCount", monthCount);
        Long yearCount = failureService.query().ge("failureTime", QDateUtils.getFirstDayOfYear())
                .le("failureTime", currentDate).count();
        resMap.put("yearCount", yearCount);
        return AjaxResult.success(resMap);
    }
}
